package com.factory.iamp.utils.helper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParamsHelper {
    private String name;
    private String type;
    private String username;
    private String role;
    private String creator;
    private String handler;
    private String timeRangeName;
    private String begin;
    private String end;
    private String tidList;

    private String SingleStringValid(String pa, String inp) {
        Pattern pattern = Pattern.compile(pa);
        Matcher matcher = pattern.matcher(inp);
        if(matcher.find()) return matcher.group(0);
        return null;
    }

    private String MultiStringValid(String inp) {
        Pattern pattern = Pattern.compile("[-+]?[0-9]+");
        Matcher matcher = pattern.matcher(inp);
        StringBuilder typeBuilder = new StringBuilder();
        while(matcher.find()) {
            if(typeBuilder.length() == 0) typeBuilder.append("(");
            else typeBuilder.append(",");
            typeBuilder.append(matcher.group(0));
        }
        typeBuilder.append(")");
        if(typeBuilder.length() > 1) return typeBuilder.toString();
        return null;
    }

    private String MakeItSave(String inp) {
        return inp.replace("\"", "\\\"")
                .replace("%", "\\%")
                .replace("_", "\\_");
    }

    public ParamsHelper() {
        this(null, null, null, null, null, null, null);
    }

    public ParamsHelper(String name, String role) {
        this();
        if(name != null) this.setUsername(name);
        if(role != null) this.setRole(role);
    }

    public ParamsHelper(
            String name, String type, String creator, String handler, String timeRangeName, String begin, String end
    ) {
        if(name != null) this.setName(name);
        if(type != null) this.setType(type);
        if(creator != null) this.setCreator(creator);
        if(handler != null) this.setHandler(handler);
        if(begin != null) this.setBegin(begin);
        if(end != null) this.setEnd(end);
        if(timeRangeName != null) this.setTimeRange(timeRangeName);
    }

    public void setName(String name) {
        this.name = "%" + MakeItSave(name) + "%";
    }

    public void setUsername(String name) {
        this.username = "%" + MakeItSave(name) + "%";
    }

    public void setRole(String role) {
        this.role = this.MultiStringValid(role);
    }

    public void setType(String type) {
        this.type = this.MultiStringValid(type);
    }

    public void setCreator(String creator) {
        this.creator = "%" + MakeItSave(creator) + "%";
    }

    public void setHandler(String handler) {
        this.handler = "%" + MakeItSave(handler) + "%";
    }

    public void setBegin(String begin) {
        this.begin = this.SingleStringValid("[0-9]{4,}-[0-9]{2}-[0-9]{2}", begin);
    }

    public void setEnd(String end) {
        this.end = this.SingleStringValid("[0-9]{4,}-[0-9]{2}-[0-9]{2}", end);
    }

    public void setTimeRange(String name) {
        if(this.begin != null && this.end != null) this.timeRangeName = name;
    }

    public void setTidList(String tidList) {
        this.tidList = this.MultiStringValid(tidList);
    }

    public String makeQueryString() {
        StringBuilder resultBuilder = new StringBuilder();
        if(this.name != null) resultBuilder.append("name like \"").append(this.name).append("\"");

        if(this.type != null) {
            if(resultBuilder.length() > 0) resultBuilder.append(" and ");
            resultBuilder.append("type in ").append(this.type);
        }

        if(this.username != null) {
            if(resultBuilder.length() > 0) resultBuilder.append(" and ");
            resultBuilder.append("realname like \"").append(this.username).append("\"");
        }

        if(this.role != null) {
            if(resultBuilder.length() > 0) resultBuilder.append(" and ");
            resultBuilder.append("role in ").append(this.role);
        }

        if(this.creator != null) {
            if(resultBuilder.length() > 0) resultBuilder.append(" and ");
            resultBuilder.append("creator in (select uid from `user` where username like \"")
                    .append(this.creator).append("\")");
        }

        if(this.handler != null) {
            if(resultBuilder.length() > 0) resultBuilder.append(" and ");
            resultBuilder.append("handler in (select uid from `user` where username like \"")
                    .append(this.handler).append("\")");
        }

        if(this.tidList != null) {
            if(resultBuilder.length() > 0) resultBuilder.append(" and ");
            resultBuilder.append("tid in ").append(this.tidList);
        }

        if(this.timeRangeName != null) {
            if(resultBuilder.length() > 0) resultBuilder.append(" and ");
            resultBuilder.append(this.timeRangeName).append(" between \"")
                    .append(this.begin).append("\" and \"").append(this.end).append("\"");
        }

        return resultBuilder.toString();
    }

    public String makeQueryStringOrder(String order, String col) {
        if(order.equals("2")) return this.makeQueryString() + " order by " + col + " desc";
        else return this.makeQueryString() + " order by " + col + " asc";
    }

}
